package com.pine.app.module.security.core.exception;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xiaoyuan
 * @create 2020/3/12 0:04
 **/
@Slf4j
public class DefaultExceptionEntryPoint extends AbstractExceptionEntryPoint {

    @Override
    public <T extends BaseException> void handler(HttpServletRequest request, HttpServletResponse response, T e) throws IOException, ServletException {

        try {
            String message = e.getMessage();
            if (StringUtils.isNotEmpty(message)) {
                log.error("业务异常：<{}>", message);
            }
            if (log.isDebugEnabled()) {
                log.error("业务异常：<{}>", message, e);
            }
            responseObject(e, request, response);
            response.flushBuffer();
        } catch (RuntimeException e1) {
            throw e1;
        } catch (Exception e1) {
            throw new RuntimeException(e1);
        }
    }
}
